<template>
  <div class='container'>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          size="small"
          @click="search_button()">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          size="small"
          @click="reset_button()">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true"  size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input  class="input-width" v-model="keyword_value" placeholder="门店" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>门店列表</span>
      <el-button size="mini" class="btn-add" @click="multiple_delete_button()" style="margin-left: 20px; float:right" icon='el-icon-delete' type="danger">批量删除</el-button>
      <el-button size="mini" class="btn-add" @click="add_button()" style="margin-left: 20px; float:right" icon='el-icon-plus' type="primary">添加</el-button>
    </el-card>
    <div class='table-container'>
      <el-table
        class='m-30-t'
        :data="shop_list"
        fit
        @select="chosen_option"
        @select-all="all_options"
        v-loading="listLoading"
        stripe
        border
        highlight-current-row>
        <el-table-column type="selection" ></el-table-column>
        <el-table-column prop="id" label="门店id" align="left">
        </el-table-column>
        <el-table-column prop="true_name" label="门店名称" align="left">
        </el-table-column>
        <el-table-column prop="address" label="门店地址" align="center">
        </el-table-column>
        <el-table-column label="门店图片" align="center">
          <template slot-scope="scope">
            <el-image :src="scope.row.logo" class='good-img' v-if="scope.row.logo"></el-image>
            <span v-else>无</span>
          </template>
        </el-table-column>
        <el-table-column label="门店类型" prop="category_name" align="center">
          <template slot-scope="scope">
            <el-tag>{{scope.row.category_name}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="是否中心店"
          align="center">
          <template slot-scope="scope">
            <span>{{scope.row.is_vip==0?"否":"是"}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="mobile"
          label="联系电话"
          align="center">
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="创建日期"
          align="center">
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width='300px'>
          <template slot-scope="scope">
            <div class=''>
              <!-- <el-button size="small" type="warning" @click="detail_button(scope.row.id)">详情</el-button> -->
              <div>
                <el-button size="small" type="danger" @click="delete_button(scope.row.id)">删除</el-button>
                <el-button size="small" type="success" @click="edit_button(scope.$index, scope.row)">编辑</el-button>
                <el-button size="small" type="warning" @click="shop_account_log_button(scope.row.id)">收入</el-button>
                <el-button size="small" type="primary"  @click="shop_finance_button(scope.row.id)">财务</el-button>
              </div>
              <div style='height:20px'></div>
              <div >
                <el-button size="small" type="primary"  @click="shop_detail_button(scope.row.id)">门店码</el-button>
                <el-button size="small" type="primary"  @click="paying_acode_button(scope.row.id)">付款码</el-button>
                <el-button size="small" type="danger" @click="biz_user_button(scope.row.id)">用户</el-button>
                <el-button size="small" type="warning" @click="shop_point_button(scope.row.id)">积分</el-button>
              </div>
              <div style='height:20px'></div>
              <div >
                <el-button size="mini" type="warning" @click="shop_member_button(scope.row.id)">店员</el-button>
                <el-button size="mini" type="warning" @click="shop_related_button(scope.row.id)">分销员</el-button>
                <el-button size="mini" type="primary" @click="order_share_log_button(scope.row.id)">佣金</el-button>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container m-50-t m-50-b">
      <span></span>
      <el-pagination
        background
        @size-change="size_change"
        @current-change="current_change"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="page_num"
        :page-size="page_size"
        :page-sizes="page_sizes"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="门店详情"
      width="50%"
      :visible.sync="detail_dialog_visible">
      <div>
        <span class="true_name">门店名称：</span>
        <span class="m-10-l ">{{detail_data.true_name}}</span>
      </div>
      <div class=' m-30-t'>
        <span class="true_name">创建时间：</span>
        <span class="m-10-l  ">{{detail_data.create_time}}</span>
      </div>
      <div class=' m-30-t'>
        <span class="true_name">联系电话：</span>
        <span class="m-10-l  ">{{detail_data.mobile}}</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="detail_dialog_visible = false" size="small">取 消</el-button>
        <el-button @click="detail_dialog_visible = false" type="primary" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="门店支付码"
      width="50%"
      :visible.sync="show_acode_dialog">
      <div class='flex j-c-c'>
        <el-image :src='show_acode_url' style="width: 300px;height300px;margin-left:50px">


        </el-image>

      </div>
      <div class='m-30-t'>图片地址: <a :href='show_acode_url' class="m-30-l" style="color:blue">{{show_acode_url}}</a> </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="show_acode_dialog = false" size="small">取 消</el-button>
        <el-button @click="show_acode_dialog = false" size="small" type="primary">确定</el-button>
        <!-- <el-button @click="acode_download" type="primary" size="small">下载</el-button> -->
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import shop_request from "@/api/shop_request"
  import acode_request from "@/api/acode_request"
  export default {
    name: 'shop_list',
    data() {
      return {
        detail_data: {},
        shop_list: [],
        dialog_visible: false,
        isEdit: false,
        search_value: '',
        page_num: 1,
        page_size: 10,
        page_sizes: [5,10,15],
        total: null,
        true_name: '',
        create_time: '',
        birthday: '',
        mobile: '',
        detail_dialog_visible: false,
        keyword: '',
        keyword_value: '',
        current_size: 10,
        listLoading: false,
        current_page: 1,
        show_acode_dialog: false,
        show_acode_url: '',
      }
    },
    created() {
      this.get_list()
    },
    mounted() {

    },
    methods: {
      // 搜索
      search_button() {
        this.current_page = 1
        this.keyword = this.keyword_value
        this.filter_list()
      },
      // 重置
      reset_button() {
        this.keyword = ''
        this.keyword_value= ''
        this.get_list()
      },
      // 添加
      add_button() {
        this.$router.push('/shop/shop_list_add')
      },
      // 删除
      delete_button(id) {
        this.$confirm('是否要进行删除操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            let params = {
              id
            }
            shop_request.delete_shop(params, res=> {
              this.get_list()
          })
        }).catch(err=>err)
          this.get_list()
      },
      chosen_option(id_list) {
        this.delete_list = id_list
      },
      all_options(id_list) {
        this.delete_list = id_list
      },
      shop_member_button(id) {
        this.$router.push({path: "/experts/shop_member", query: {shop_id:id}})
      },
      shop_related_button(id) {
        this.$router.push({path: "/experts/shop_relate", query: {shop_id:id}})
      },
      order_share_log_button(id) {
        this.$router.push({path: "/experts/order_share_log", query: {shop_id:id}})
      },
      multiple_delete_button() {
        this.$confirm('是否要进行批量删除操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          for (let item of this.delete_list) {
            shop_request.delete_shop({id: item.id}, res=> {
              this.get_list()
            })
          }
          this.delete_list = []
        }).catch(err=>err)
      },
      // 编辑
      edit_button(index, item) {
        this.$router.push({path: '/shop/shop_list_edit', query: {id: item.id}})
      },
      // 详情
      detail_button(id) {
        this.$router.push({path: '/shop/shop_list_detail', query:{id}})

      },
      size_change(current_size) {
        this.current_size = current_size
        this.get_list()
      },
      shop_finance_button(id) {
        this.$router.push({path: '/shop/shop_finance', query: {id}})

      },
      biz_user_button(id) {
        this.$router.push({path: '/shop/biz_user', query: {id}})

      },
      acode_download() {
        if (!this.show_acode_url) {
          return
        }
        var canvas = document.createElement('canvas')
        var img = document.createElement('img')
        img.onload = function(e) {
          canvas.width = img.width
          canvas.height = img.height
          var context = canvas.getContext('2d')
          context.drawImage(img, 0, 0, img.width, img.height)
          canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height)
          canvas.toBlob((blob) => {
            const link = document.createElement('a')
            link.href = window.URL.createObjectURL(blob)
            link.download = '支付码'
            link.click()
          }, 'image/jpeg')
        }
        img.setAttribute('crossOrigin', 'Anonymous')
        img.src = this.show_acode_url
      },
      shop_point_button(id) {
        this.$router.push({path: '/shop/shop_point', query: {id}})

      },
      paying_acode_button(id) {
        this.show_acode_dialog = true
        let sid = id + 'b11b'
        let params = {
          sid
        }
        acode_request.wxacodeshow(params, res=> {
          this.show_acode_url = res.data.img
        })

      },
      shop_detail_button(id) {
        this.show_acode_dialog = true
        let sid = id + 'b'
        let params = {
          sid
        }
        acode_request.wxacodeshow(params, res=> {
          this.show_acode_url = res.data.img
        })

      },
      shop_account_log_button(id) {
        this.$router.push({path: '/shop/shop_account_log', query: {id}})

      },
      current_change(current_page) {
        this.current_page = current_page
        this.get_list()
      },
      filter_list() {
        shop_request.shop_list({pageSize: 1000000000, key: this.keyword}, res=> {
          this.shop_list = res.data
          this.total = res.data.length
        })
      },
      get_list() {
        this.listLoading = true,
        shop_request.shop_list({pageSize: this.current_size, page: this.current_page, key: this.keyword}, res=> {
          this.listLoading = false,
          this.shop_list = res.data
          this.total = res.totalRecord
        })
      },

    }
  }
</script>

<style scoped>
@import "~@/styles/layout.scss";
.table-container {
  margin-left: 30px;
}
.pagination-container {
  float: right;
}
.good-img {
  width: 100px;
  height:100px;
}
</style>
